/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CapaPresentacion;

import CapaLogica.Gestor;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Elayner
 */
public class PantallaMecenazgoAgregar extends javax.swing.JFrame {
    
    private static ArrayList <TreeMap> listaArtistas; 
    private static TreeMap datosArtista;
    private Gestor gestor=new Gestor();
    private int idMecena;
    private PantallaMecenaEditar pantallaBack;
    private Date fechaActual=new Date();
    

    /**
     * Creates new form PantallaMecenazgoAgregar
     */
    public PantallaMecenazgoAgregar() {
        initComponents();
    }
    
    public PantallaMecenazgoAgregar(int pidMecena, PantallaMecenaEditar pedit) throws Exception {
        initComponents();
        iniListAristas(pidMecena);
        idMecena=pidMecena;
        pantallaBack=pedit;
        fec_ini.setMaxSelectableDate(fechaActual);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        list_artistas = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        fec_fin = new com.toedter.calendar.JDateChooser();
        fec_ini = new com.toedter.calendar.JDateChooser();
        btn_aceptar = new javax.swing.JButton();
        btn_cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Lista Artistas");

        jLabel2.setText("Fecha inicio:");

        jLabel3.setText("Fecha fin:");

        fec_ini.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fec_iniPropertyChange(evt);
            }
        });

        btn_aceptar.setText("Aceptar");
        btn_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_aceptarActionPerformed(evt);
            }
        });

        btn_cancelar.setText("Cancelar");
        btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(fec_ini, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btn_aceptar)
                                .addGap(18, 18, 18)
                                .addComponent(btn_cancelar)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(fec_fin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(list_artistas, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(83, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(list_artistas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(fec_ini, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(fec_fin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_aceptar)
                    .addComponent(btn_cancelar))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btn_cancelarActionPerformed

    private void btn_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_aceptarActionPerformed
        if(validarPantalla()){
            try {
                int index=list_artistas.getSelectedIndex();
                int idArtista=(int)listaArtistas.get(index).get("id");
                String fecha_inicio=helperUI.getDateFormatConv(fec_ini.getDate());
                String fecaha_fin=helperUI.getDateFormatConv(fec_fin.getDate());
                
                if(gestor.registrarMecenazgo(idMecena, idArtista, fecha_inicio, fecaha_fin)){
                    this.setVisible(false);
                     pantallaBack.loadScreenMecena(idMecena);
                }

                    
            } catch (Exception ex) {
                Logger.getLogger(PantallaMecenazgoAgregar.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{
            JOptionPane.showMessageDialog(this,(String) "Debe de llenar los campos requeridos","Error",JOptionPane.ERROR_MESSAGE);
        } 
    }//GEN-LAST:event_btn_aceptarActionPerformed

    private void fec_iniPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fec_iniPropertyChange
        fec_fin.setMinSelectableDate(fec_ini.getDate());
    }//GEN-LAST:event_fec_iniPropertyChange

   public void iniListAristas(int idMecena) throws Exception{        
        listaArtistas = gestor.listarArtistaDisponibles(idMecena);

        for (int i=0; i<listaArtistas.size(); i++) {
            datosArtista =(TreeMap)listaArtistas.get(i);
            list_artistas.addItem(" "+datosArtista.get("id")+"-"+datosArtista.get("nombre"));
        }
    }
   
   
   public boolean validarPantalla(){
       boolean result=true;
       
       if(fec_fin.getDate()==null)
           result=false;
       if(fec_ini.getDate()==null)
           result=false;
       
       return result;
   }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaMecenazgoAgregar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaMecenazgoAgregar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaMecenazgoAgregar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaMecenazgoAgregar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaMecenazgoAgregar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_aceptar;
    private javax.swing.JButton btn_cancelar;
    private com.toedter.calendar.JDateChooser fec_fin;
    private com.toedter.calendar.JDateChooser fec_ini;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JComboBox list_artistas;
    // End of variables declaration//GEN-END:variables
}
